/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo.helper;
import java.util.ArrayList;
import java.util.Iterator;
import modelo.DTO.DTODataMining;
import modelo.DTO.DTOVisitasAlumno;
import modelo.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
 *
 * @author Ion B. Padilla
 * @version 1.1
 */
public class WikiHelper {
    private static WikiHelper instance;

    public WikiHelper() {
    }

    public static WikiHelper getInstance() {
        if (null == instance) {
            instance = new WikiHelper();
        }
        return instance;
    }
    /**
     * Metodo que devuelve numero de ediciones que ha tenido una wiki
     * @param nombre el nombre del alumno
     * @param curso el curso en el que se va a consultar
     */
    public DTODataMining getEdicionWikiPorStudentPorCurso(String nombre, int curso) {
        DTOVisitasAlumno dtoVisitas = null;
        Transaction tx = null;
        try {
            //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Session session = HibernateUtil.getInstance().getSession();
            tx = session.beginTransaction();
            Iterator results = session.createQuery("SELECT user.username as username, COUNT(log.time) as edits "
                    + "FROM MdlLog log, MdlCourseModules cm,MdlUser user  "
                    + "WHERE log.cmid = cm.id "
                    + "AND log.userid = user.id "
                    + "AND log.action = 'edit' "
                    + "AND log.course="+curso+" "
                    + "AND user.username='"+nombre+"' "
                    + "AND cm.module = (SELECT id FROM MdlModules WHERE name='wiki') "
                    + "GROUP BY user.id ").list().iterator();
            while (results.hasNext()) {
                Object[] row = (Object[]) results.next();
                dtoVisitas = new DTOVisitasAlumno((String) row[0], Long.toString((Long) row[1]));
            }
            tx.commit(); session.close();
            return dtoVisitas;
        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            e.getStackTrace();
            return new DTOVisitasAlumno();
        }
    }
    /**
     * Metodo que devuelve el numero de ediciones que ha tenido una wiki
     * @param curso el curso del que queremos consultar
     */
    public ArrayList<DTODataMining> getEdicionWikiPorStudentsPorCurso(int curso){
        Transaction tx = null;
        ArrayList<DTODataMining> lista = new ArrayList<DTODataMining>();
        try {
            //Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Session session = HibernateUtil.getInstance().getSession();
            tx = session.beginTransaction();
            Iterator results = session.createQuery("SELECT user.username as username, COUNT(log.time) as edits "
                    + "FROM MdlLog log, MdlCourseModules cm,MdlUser user  "
                    + "WHERE log.cmid = cm.id "
                    + "AND log.userid = user.id "
                    + "AND log.action = 'edit' "
                    + "AND log.course="+curso+" "
                    + "AND cm.module = (SELECT id FROM MdlModules WHERE name='wiki') "
                    + "GROUP BY user.id ").list().iterator();
            while (results.hasNext()) {
                Object[] row = (Object[]) results.next();
                DTOVisitasAlumno alumno = new DTOVisitasAlumno((String) row[0], Long.toString((Long) row[1]));
                lista.add(alumno);
            }
            tx.commit(); session.close();
            return lista;
        } catch (HibernateException e) {
            System.out.println(e.getMessage());
            e.getStackTrace();
            return new ArrayList<DTODataMining>();
        }
    }
}
